package com.youan.log.modules.audit.dataanalysis;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
import org.springblade.core.log.exception.ServiceException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class IP2Region {
    private DbSearcher dbSearcher;

    public IP2Region() {
        this.dbSearcher = getDbSearch();
    }

    public void close() {
        try {
            this.dbSearcher.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Region ip2Region(String ipAddress) {
        Region result = new Region();
        //中国|0|香港|0|阿里云
        String region = null;
        try {
            DataBlock dataBlock = dbSearcher.memorySearch(ipAddress);
            region = dataBlock.getRegion();
        } catch (IOException ioException) {
            return result;
        }
        String[] regions = region.split("\\|");

        if(regions.length != 5) {
            return result;
        }

        String country = regions[0];
        if (!StringUtils.equals(country, "0")) {
            result.setCountry(country);
        }

        String province = regions[2];
        if (!StringUtils.equals(province, "0")) {
            result.setProvince(province);
        }

        String city = regions[3];
        if (!StringUtils.equals(city, "0")) {
            result.setCity(city);
        }

        String isp = regions[4];
        if (!StringUtils.equals(isp, "0")) {
            result.setIsp(isp);
        }
        return result;
    }


    private DbSearcher getDbSearch() {
        Resource resource = new ClassPathResource("ip2region/ip2region.db");
        //判断文件是否存在：
        if (!resource.exists()) {
            throw new ServiceException("程序中不存在ip2region.db，请联系开发人员!!");
        }
        Path path = Paths.get(System.getProperty("java.io.tmpdir"), "ip2region.db");
        if(Files.notExists(path)) {
            try {
                Files.copy(resource.getInputStream(), path);
            } catch (IOException ioException) {
                throw new ServiceException("向临时文件夹中写入ip2region.db失败，请联系开发人员!!");
            }
        }

        try {
            DbConfig config = new DbConfig();
            System.out.println(path.toString());
            DbSearcher searcher = new DbSearcher(config, path.toString());
            return searcher;
        } catch (DbMakerConfigException | FileNotFoundException e) {
            throw new ServiceException("初始化[DbSearcher]失败，请联系开发人员!");
        }
    }

    @Data
    public static class Region{
        private String country;
        private String province;
        private String city;
        private String isp;
    }


}
